package com.UI;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

import com.Model.StudentDBModel;

public class EditFacultyHours extends Activity{
	private String facultyType;
	private String comboID;
	private StudentDBModel model;
	private ArrayList facultyHoursDetails;
	private String[] facultyDetails;
	private ContentValues editedValues;
	private int dbFacultyType;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edit_faculty_hours);
		
		Bundle extras = getIntent().getExtras();
		comboID = extras.getString("FacCourseComboID");
		model = new StudentDBModel(this);
		facultyHoursDetails = model.getFacultyHoursDetails(comboID);
		facultyDetails = (String[]) facultyHoursDetails.toArray(new String[facultyHoursDetails.size()]);
		
		//create the spinner to hold the list of Class events that can be added
		Spinner facultyTypeSpinner = (Spinner) findViewById(R.id.addFacultyTypeSpinner);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.AddFacultyTypeArray, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		facultyTypeSpinner.setAdapter(adapter);
		//set the spinner to the type retrieved from the DB
		if(facultyDetails[2].compareTo("Professor") == 0){
			dbFacultyType = 1;
			facultyType = "Professor";
		}
		else if(facultyDetails[2].compareTo("Instructor") == 0){
			dbFacultyType = 2;
			facultyType = "Instructor";
		}
		else if(facultyDetails[2].compareTo("Teaching Assistant (TA)") == 0){
			dbFacultyType = 3;
			facultyType = "Teaching Assistant (TA)";
		}
		else if(facultyDetails[2].compareTo("Graduate Assistant (GA)") == 0){
			dbFacultyType = 4;
			facultyType = "Graduate Assistant (GA)";
		}
		else{
			dbFacultyType = 0;
			facultyType = "";
		}
		facultyTypeSpinner.setSelection(dbFacultyType);

		// Set up a call back for the spinner in case it gets changed
		facultyTypeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onNothingSelected(AdapterView<?> arg0) {
				// Do nothing.
			}

			public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
				final Spinner selectedType = (Spinner) findViewById(R.id.addFacultyTypeSpinner);
				facultyType = selectedType.getSelectedItem().toString();
			}
		});
		
		TextView lblFacultyNameFromDB = (TextView) findViewById(R.id.lblFacultyNameFromDB);
		lblFacultyNameFromDB.setText(facultyDetails[1]);
		
		EditText txtFacLocation = (EditText) findViewById(R.id.txtFacLocation);
		txtFacLocation.setText(facultyDetails[3]);

		EditText txtFacHours = (EditText) findViewById(R.id.txtFacHours);
		txtFacHours.setText(facultyDetails[4]);
		
		// register this activity as a button event listener
        Button btnSubmit = (Button)findViewById(R.id.btnFacHoursInfoSubmit);
        btnSubmit.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
        		if(facultyType.length() == 0){
			 		   AlertDialog alertDialog = new AlertDialog.Builder(EditFacultyHours.this).create();	        			
			 		   alertDialog.setTitle("Warning");	        		  
			 		   alertDialog.setMessage("Please select a Faculty Type");	        		  	        		  
			 		   alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
			 			   public void onClick(DialogInterface dialog, int which) {
			 				   return;
			 			   } 
			 		   }); 
					  alertDialog.show();
				}
				else{
	        		// collect the form data
	        		editedValues = new ContentValues();
	        		
	        		editedValues.put("FacType", facultyType);
	
	        		EditText txtFacLocation = (EditText) findViewById(R.id.txtFacLocation);
	        		editedValues.put("FacLocation", txtFacLocation.getText().toString());
	
	        		EditText txtFacHours = (EditText) findViewById(R.id.txtFacHours);
	        		editedValues.put("FacHours", txtFacHours.getText().toString());
	
		    		// use the model to store the course data
		    		model.updateFacultyHours(editedValues, comboID);
		    		
		    		TextView lblResults = (TextView) findViewById(R.id.lblEditFacHoursResult);
					lblResults.setText(getString(R.string.FacHoursEdited));
	
					//finish the activity to return to the calling event
					EditFacultyHours.this.finish();	
				}
        	}           
        });
        
     // register this activity as a button event listener
		Button btnGoBack = (Button) findViewById(R.id.btnBack);
		btnGoBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View component) {
				// go back to the pick class event view
				EditFacultyHours.this.finish();
			}
		});
		
		Button btnGoHome = (Button) findViewById(R.id.btnHome);
		btnGoHome.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View component) {
				// go to the home page
				Intent goHome = new Intent(EditFacultyHours.this, HomePage.class);
				startActivity(goHome);
				//finish the activity to return to the calling event
				EditFacultyHours.this.finish();
			}
		});
	}
}
